<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tool :obj="company" :isshow="false" @additem="adddeparts" />
        <!-- 树形结构 -->
        <el-tree :data="treeData" :props="defaultProps" :default-expand-all="true" @node-click="handleNodeClick">
          <tree-tool slot-scope="{data}" :obj="data" @additem="adddeparts" @editDept="editon" />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="depts" v-model="dialogVisible" :obj="additemdata" @onload="getdepartments" @onsuccess="getdepartments" />
  </div>
</template>

<script>
import treeTool from './components/tree-tool'
import { getdepartments } from '@/api/departments'
import { transListToTree } from '@/utils'
import AddDept from './components/add-dept'
export default {
  components: { treeTool, AddDept },
  data() {
    return {
      treeData: [

      ],
      defaultProps: {
        label: 'name'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      dialogVisible: false,
      additemdata: {}
    }
  },

  created() {
    this.getdepartments()
  },

  methods: {
    handleNodeClick(data) {
      // console.log(data)
    },
    async  getdepartments() {
      const res = await getdepartments()
      this.treeData = transListToTree(res.depts, '')
    },
    adddeparts(val) {
      this.dialogVisible = true
      console.log(val)
      this.additemdata = val
    },
    editon(obj) {
      this.dialogVisible = true
      this.additemdata = obj
      this.$refs.depts.geteditdepts(obj.id)
    }
  }
}

</script>

<style scoped lang='scss'>

</style>
